<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- el-tree  树形图 -->
        <el-tree :data="list" :props="{children:'children',label:'name'}">
          <!-- 使用作用域插槽获取组件内部的数据, -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item>删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- diglog  弹层框 -->
      <el-dialog
        title="新增或者编辑"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <deptDiglog :id="curId" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import deptDiglog from '@/views/departments/deptDiglog.vue'
export default {
  components: {
    deptDiglog
  },
  data() {
    return {
      list: [], // 获取到部门数据存储在这
      showDialog: false, // 弹层框的显示和隐藏时
      curId: '', // 当前点击的id
      isEdit: false // 如果时false 就是新增  true 是编辑
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 子传父收
    hSuccess() {
      // 重新渲染页面 关闭弹层
      this.loadDepartment()
      this.showDialog = false
    },
    // 编辑
    hEdit(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
    },
    // 新增
    hAdd(id) {
      this.isEdit = false
      this.curId = id
      this.showDialog = true
    },
    // 加载部门列表
    async loadDepartment() {
      try {
        const res = await getDepartment()
        // 第一行有了 直接删除第一行
        res.data.depts.shift()
        // 项目中实现将平铺的数据转化树形的结构
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }

}
</script>
